package com.yunhe.option.repository.data;

import com.yunhe.option.domain.data.SeasonPrice;
import com.yunhe.option.domain.data.SeasonPriceDetail;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface SeasonPriceDetailRepository extends Neo4jRepository<SeasonPriceDetail, Long> {

//    @Query("start s=Node({seasonId}) match (d:SeasonPrice{code:{seasonCode}}) create (s)-[:HAS_DETAIL]->(d)")
    @Query("start s=Node({seasonId}), d=Node({detailId}) create (s)-[:HAS_DETAIL]->(d)")
    void bindSeasonPrice(@Param("detailId") Long detailId, @Param("seasonId") Long seasonPriceId);

    @Query("start d=Node({detailId}) match (p:PriceRate) where p.code in {priceRateCode} create (d)-[:PRICERATE_OF]->(p)")
    void bindPriceRate(@Param("detailId") Long detailId, @Param("priceRateCode") Long priceRateCode);

    @Query("start s=Node({seasonPriceId}) match (s)-[:HAS_DETAIL]-(d:SeasonPriceDetail) return d")
    Iterable<SeasonPriceDetail> findBySeasonPriceID(@Param("seasonPriceId") Long seasonPriceId);

    @Query("start s=Node({seasonPriceId}) match (s)-[]->(d:SeasonPriceDetail), (s)-[]->(pr:PriceRate)<-[]-(d) where pr.code = {priceRateCode} and d.priceRateCode = {priceRateCode} return d")
    Iterable<SeasonPriceDetail> findBySeasonPriceIDAndPriceRateCode(@Param("seasonPriceId") Long seasonPriceId, @Param("priceRateCode") Long priceRateCode);

    @Query("start d=Node({id}) match (s:SeasonPrice)-[:HAS_DETAIL]->(d) return s")
    SeasonPrice findSeasonPriceByDetailId(@Param("id") Long id);

    @Query("start s=Node({seasonPriceId}) match (s)-[]->(pr:PriceRate)<-[]-(d:SeasonPriceDetail) where pr.code = {priceRateCode} and d.priceRateCode = {priceRateCode} set d.price = {price}")
    void updateAllPrice(@Param("seasonPriceId") Long seasonPriceId, @Param("priceRateCode") Long priceRateCode, @Param("price") Double price);

    @Query("start s=Node({seasonPriceId}) match (s)-[]-(d:SeasonPriceDetail)-[]-(r:PriceRate) where r.code = {priceRateCode} detach delete d")
    void deleteBySeasonPriceIdAndPriceRateCode(@Param("seasonPriceId") Long seasonPriceId, @Param("priceRateCode") Long priceRateCode);

    @Query("start s=Node({seasonPriceId}) match (s)-[]-(d:SeasonPriceDetail) detach delete d, s")
    void deleteBySeasonPriceId(@Param("seasonPriceId") Long seasonPriceId);
}
